﻿Shader "Dan/UI/UI_StarShinny"
{
    Properties
    {
        _Color ("Color", Color) = (0.07843138,0.3921569,0.7843137,1)
        _Emission ("Emission", Float ) = 1
        _Density ("Density", Float ) = 8
        
        _Amount ("Amount", Range(0, 1)) = 0.5
        _Size ("Size", Range(0, 1)) = 0.7693831
        _RandomSpeed ("RandomSpeed", Float ) = 1

    }
    
    SubShader
    {

        Tags { "IgnoreProjector"="True"
            "Queue"="Transparent"
            "RenderType"="Transparent" }
        LOD 100    
        Blend SrcAlpha OneMinusSrcAlpha
        ZWrite Off 
 
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            #include "UnityCG.cginc"
            #include "../ShaderLibs/UIFX.cginc"
            #include "../ShaderLibs/UVOperator.cginc"
            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };
            
            struct v2f
            {
                float4 vertex : SV_POSITION;    
                float2 uv : TEXCOORD0;
                
            };

             float4 _Color;
             float _Density;
             
             float _RandomSpeed;
             float _Amount;
             float _Size;
             float _Emission;
            
            v2f vert ( appdata v )
            {
                
                v2f o;
                UNITY_INITIALIZE_OUTPUT(v2f ,o);
                o.vertex = UnityObjectToClipPos(v.vertex);
                
                o.uv = v.uv;        
                return o;
            }
            
            void ShinnyShape(float2 uv , float randomSpeed, float density,float shape,out float mask ,out float shapeShinny){
                float rdSpeed = _Time.x*_RandomSpeed*0.001;
                float2 shapeUV = floor(uv*_Density)/(_Density-1);
                //根据UV划分求出两种随机值
                float2 rd1 = shapeUV+rdSpeed*0.5;                
                float noise01 = Flick(rd1);
                float2 rd2 = shapeUV+rdSpeed;                
                float noise02 = Flick(rd2); 
                shapeShinny = saturate(shape-noise01*0.4)-noise02;
                float2 suv = abs(shapeUV-0.5);
                mask = step(saturate((1-(1-suv.x)*(1-suv.y))),0.6);
            
                }


            fixed4 frag (v2f i ) : SV_Target
            {
                
                
                float2 uv = i.uv;
                
                // float mask01;
                // float shapeshinny;
                // float fff = UVMaskHoneycomb(uv,12,0.5);
                // ShinnyShape(uv,0.5,12,fff,mask01,shapeshinny);
                // return shapeshinny*mask01;

                float rdSpeed = _Time.x*_RandomSpeed*0.001;
                float2 starUV = floor(uv*_Density)/(_Density-1);
                //根据UV划分求出两种随机值
                float2 rd1 = starUV+rdSpeed*0.5;                
                float noise01 = Flick(rd1);
                float2 rd2 = starUV+rdSpeed;                
                float noise02 = Flick(rd2);
                
                //UV划分开方
                float2 ruv = sqrt(abs((frac(uv*_Density)-0.5)));
                //求星形状
                float shape =1-saturate(ruv.x+ruv.y);
                //根据随机值闪烁               
                float starShin = saturate(shape-noise01*0.4)-noise02;
                
                //遮罩
                float2 suv = abs(starUV-0.5);
                float mask = step(saturate((1-(1-suv.x)*(1-suv.y))),0.6);
                //星星大小
                float starSize = step(1 - _Size*0.9,starShin);
                //随机值大小
                float noiseAmount = step(noise02,_Amount);
                //剔除
                clip(starSize*noiseAmount*mask - 0.5);
                //颜色
                float star = saturate(starShin);               
                float3 finalColor = (_Color.rgb+star*star)*_Emission;
                 
                return fixed4(finalColor,1);
                
            }
            ENDCG
        }
    }
    
    
    
}
